
from pytrends.request import TrendReq
import matplotlib.pyplot as plt
import time
import sys
import pandas as pd
import datetime as dt

def get_day_num(date):
    mo, d, y = map(int, date.split('/'))
    day_num = (dt.date(y, mo, d) - dt.date(y,1,1)).days
    return day_num

def get_day_str(num, year):
    day_str = (dt.datetime(year,1,1) + dt.timedelta(days=num)).strftime("%-m/%-d/%Y")
    return day_str

def get_years(day1, day2, year1, year2):
    if day1 > 357: 
        year_o, year_e = year1, year2
    elif 1 <= day1 <= day2 <= 60:
        year_o, year_e = year2, year2
    else:
        year_o, year_e = year1, year1
    return year_o, year_e

def get_data_trends(pred, term, area, y_start, y_end, mo_start, mo_end, d_start, d_end, h_start, h_end):
    data = pred.get_historical_interest(term, year_start=y_start, month_start=mo_start, day_start=d_start, hour_start=h_start,
                                        year_end=y_end, month_end=mo_end, day_end=d_end, hour_end=h_end, cat=0, geo=area, gprop='', sleep=0)
    return data

def get_data_same_year(pred, term, area, date_earliest, date_latest):
    day_start_num = get_day_num(date_earliest)
    day_end_num = get_day_num(date_latest)
    year = int(date_earliest.split('/')[2])
    period_length = range(1,(day_end_num-day_start_num+1)+1)
    all_data = []
    for i in period_length:
        mo_start, d_start, y_start = map(int, get_day_str(day_start_num, year).split('/'))
        mo_end, d_end, y_end = map(int, get_day_str(day_start_num+8, year).split('/'))
        d = get_data_trends(pred, term, area, y_start, y_end, mo_start, mo_end, d_start, d_end, 0, 0)
        all_data.append(d.iloc[:,0:1])
        if get_day_num(get_day_str(day_start_num+8, year)) == day_end_num:
            break
        else:
            day_start_num += 1
            sys.stdout.write("\rCollection %i" % i)
            sys.stdout.flush()
            time.sleep(30) #Easily rate limited with only 10
    return all_data

def get_data_diff_year(pred, term, area, date_earliest, date_latest):
    '''
    '''
    day_start_num = get_day_num(date_earliest)
    day_end_num = get_day_num(date_latest)
    year1 = int(date_earliest.split('/')[2])
    year2 = int(date_latest.split('/')[2])
    length = (365-day_start_num) + day_end_num
    period_length = range(0,(length+1)) # range of days (length)
    day_start, day_end = day_start_num, day_start_num+8 # only first collection
    all_data = []
    for i in period_length:
        year_o, year_e = get_years(day_start, day_end, year1, year2)
        mo_start, d_start, y_start = map(int, get_day_str(day_start, year_o).split('/'))
        mo_end, d_end, y_end = map(int, get_day_str(day_end, year_e).split('/'))
        d = get_data_trends(pred, term, area, y_start, y_end, mo_start, mo_end, d_start, d_end, 0, 0)
        all_data.append(d.iloc[:,0:1])
        if get_day_num(get_day_str(day_end, year_e)) == day_end_num:
            break
        else:
            day_start += 1
            day_end += 1
            if day_start == 358:
                day_end = 1
            if day_start == 366:
                day_start = 1
            sys.stdout.write("\rCollection %i" % i)
            sys.stdout.flush()
            time.sleep(30) #Easily rate limited with only 10
    return all_data

def get_data(pred, term, area, date_earliest, date_latest):
    year1 = int(date_earliest.split('/')[2])
    year2 = int(date_latest.split('/')[2])
    if year1==year2:
        all_data = get_data_same_year(pred, term, area, date_earliest, date_latest)
    else:
        all_data = get_data_diff_year(pred, term, area, date_earliest, date_latest)
    return all_data















